<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/12/20
 * Time: 13:07
 */
require_once(dirname(__FILE__) . '/include/config.inc.php');


for ($i = 1; $i < 1628; $i++) {
    $num = str_pad($i, 4, 0, STR_PAD_LEFT);
    $num = "豫B2-2020" . $num;
    $num_arr[] = $num;

}



foreach ($num_arr as $key=>$val){
    $res = https_curl_json($num_arr[$key]);
//    if ($res == "ok" && $k < 1628) {
//        $k++;
//        $res = https_curl_json($k);
//    }

}


function https_curl_json($data)
{
    global $dosql;
    $data = ['num' => $data, 'type' => 'xuke'];
    $url = "https://dxzhgl.miit.gov.cn/dxxzsp/corpinfo/getcorpinfo.wf?pageNum=1&pageSize=10";
    //$data=json_encode($data,JSON_UNESCAPED_UNICODE);
    $data2 = http_build_query($data);
    //var_dump($data2);

    $header[] = "Accept: application/json, text/javascript, */*; q=0.01";
    $header[] = "Accept-Encoding: gzip, deflate";
    $header[] = "Accept-Language: zh-CN,zh;q=0.9,en;q=0.8";
    $header[] = "Cache-Control: no-cache";
    $header[] = "Connection: keep-alive";
    //$header[]="Content-Length: ".$str_leng;
    $header[] = "Content-Type: application/x-www-form-urlencoded; charset=UTF-8";
    $header[] = "Cookie: jsessionid=rBQJaxroX97IImalmLqp0kOshpC3udRD4akA; UM_distinctid=1763b91e435209-0be9684443cd31-930346c-1fa400-1763b91e4361bb; route=1608435746.15.9732.429865";
    $header[] = "Host: dxzhgl.miit.gov.cn";
    $header[] = "Origin: https://dxzhgl.miit.gov.cn";
    $header[] = "Pragma: no-cache";
    $header[] = "Referer: https://dxzhgl.miit.gov.cn/dxxzsp/xkz/xkzgl/resource/qiyesearch.jsp?num=%25E8%25B1%25ABB2-20201631&type=xuke";
    $header[] = "Sec-Fetch-Dest: empty";
    $header[] = "Sec-Fetch-Mode: cors";
    $header[] = "Sec-Fetch-Site: same-origin";
    $header[] = "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36";
    //$header[]="X-Requested-With: XMLHttpRequest";


    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data2);
    curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    $res = curl_exec($curl);
    if (curl_errno($curl)) {
        echo 'Errno' . curl_error($curl);//捕抓异常
    }
    curl_close($curl);

    $time = time();
    $res = json_decode($res, true);
    if (is_array($res)) {
        $arr = $res['listyj'];
        foreach ($arr as $key => $val) {
            var_dump($val);
            $license_no = $val['license_no'];
            $ywzl = $val['ywzl'];
            $fgfw = $val['fgfw'];
            $lic_id = $val['lic_id'];
            $company_name = $val['company_name'];
            $sql2 = "insert into `#@__icp`(license_no,ywzl,fgfw,lic_id,company_name,`posttime`) values ('$license_no','$ywzl','$fgfw','$lic_id','$company_name','$time')";
            $rrrr = $dosql->Execute($sql2);
            return "ok";

        }
    }

}